/**
 * Created By liyuefeng 2016/10/25 菜单 删除 查询 编辑
 */  
var currentId;
var moduleTable;

$(document)
		.ready(
				function() {
					var url = getRoot() + "/menuAdmin/searchMenuList";
					moduleTable = $("#menu-table-container").ltsTable({
						url : url,
						templateId : 'menu-table'
					});
					moduleTable.post({status:2}, 1);
					LTS.colFormatter.optFormat = function(v, row) {
						return '<a href="javascript:;" class="update-btn"><span class="label label-success" style="font-size: 14px"><i class="fa fa-edit"></i>编辑</span><span class="hidden lts-data">'
								+ JSON.stringify(row)
								+ '</span></a>&nbsp;'
								+ '<a href="javascript:;" class="remove-btn"><span class="label label-primary" style="background-color: #DD6B55;font-size: 14px"><i class="fa fa-trash-o"></i>删除</span></a>&nbsp;';
					}

					clickRemoveButton();
					confirmClickDelButton();
					updateClickButton();
					confirmUpateClickButton();
					clickSearchButton();
				});

/**
 * 点击删除按钮
 */
function clickRemoveButton() {
	$(document).on("click", ".remove-btn", function() {
		currentId = getCurrentId($(this));
		$("#remove-tip-menu").modal("show");
	});
}

/**
 * 点击确认删除按钮
 */
function confirmClickDelButton() {
	$(document).on("click", "#confirm-remove-btn", function() {
		$("#cancel-remove-btn").trigger("click");

		var params = {};
		params['id'] = parseInt($.trim(currentId));
		var url = getUrlBySuffix("/menuAdmin/update");
		params['status'] = 0;
		$.postHttp(params, url, function(json) {
			if (json==1) {
				swal({
					title : "提示",
					text : "菜单删除成功",
					type : "success",
					confirmButtonText : "确定"
				}, function() {
					location.reload();
				});
			} else {
				swal("失败", "未能从服务器获取正确信息", 'error');
			}
		});
		currentId = "";
	});
}

/**
 * 编辑数据按钮
 */
function updateClickButton() {
	$(document).on("click", ".update-btn", function() {
		var moduleJson = $(this).find("span.lts-data").html();
		moduleJson = $.trim(moduleJson);
		var module = JSON.parse(moduleJson);
		$.each($('#editForm').parent().find(".form-control"), function() {
			var name = $(this).attr("name");
			if (isNull(name)) {
				return true;
			}
			name = $.trim(name);
			var value = module[name];
			$(this).val(value);
		});
		
		$.post(getUrlBySuffix("/menuAdmin/getAllModule"), {},
				function(data) {
					var list = eval(data);
					var _html = "";
					var thishtml = $("#modelSelect").children().first().prop(
							"outerHTML");
					for (var i = 0; i < list.length; i++) {

						var _tempHtml = "<option id='" + list[i].id
								+ "' value='" + list[i].id + "' data-tokens='" + list[i].name + "'>"
								+ list[i].name + "</option>";
						_html += _tempHtml;
					}
					$("#modelSelect").html("");
					$("#modelSelect").html(thishtml + _html);
					$("#modelSelect").attr("data-live-search", "true");
					$("#modelSelect").selectpicker('refresh');
				});
		$(".menuSelectUrl").show();
		$(".modelSelect").show();
		$("#menu-edit-modal").modal("show");
		
	});
	
	$("#modelSelect").on("change",function(){
		var thisModuleId = $(this).find("option:selected").attr("id");
		$.post(getUrlBySuffix("/menuAdmin/getFunctionByModuleId"), {moduleId:thisModuleId},
				function(data) {
					var list = eval(data);
					var _html = "";
					var thishtml = $("#functionSelect").children().first().prop(
							"outerHTML");
					for (var i = 0; i < list.length; i++) {
						if(list[i].methodType==0){
							var _tempHtml = "<option id='" + list[i].id
							+ "' value='" + list[i].id + "' data-tokens='" + list[i].name + "' functionUrl='"+list[i].url+"'>"
							+ list[i].name + "</option>";
							_html += _tempHtml;
						}
						
					}
					$("#functionSelect").html("");
					$("#functionSelect").html(thishtml + _html);
					$("#functionSelect").attr("data-live-search", "true");
					$("#functionSelect").selectpicker('refresh');
				});
		$(".functionSelect").show();
		
	})
	
	$("#functionSelect").on("change",function(){
		var thisFunctionId = $(this).find("option:selected").attr("id");
		var thisFunctionUrl= $(this).find("option:selected").attr("functionUrl");
		$("input[name=menuUrl]").val(thisFunctionUrl);
		
	})
}

/**
 * 点击编辑确认按钮
 */
function confirmUpateClickButton() {
	$(document).on("click", "#edit-btn", function() {
		var params = {};
		$.each($('#editForm').parent().find(".form-control"), function() {
			var name = $(this).attr("name");
			if (isNull(name)) {
				return true;
			}
			var value = $(this).val();
			if (!isNull(value)) {
				value = $.trim(value);
				params[name] = value;
			}
		});
		var url = getRoot() + "/menuAdmin/update";
		$.postHttp(params, url, function(json) {
			if (json == 1) {
				swal({
					title : "提示",
					text : "菜单修改成功",
					type : "success",
					confirmButtonText : "确定"
				}, function() {
					location.reload();
				});
				$("#menu-edit-modal").modal("hide");

				$("#searchBtn").trigger("click");
			} else {
				swal("失败", "未能从服务器获取正确信息", 'error');
			}
		});
	});
}

/**
 * 点击搜索
 * 
 */
function clickSearchButton() {
	$(document).on("click", "#searchBtn", function() {
		var params = {};
		$.each($('#menu-query-form').find(".form-control"), function() {
			var name = $(this).attr("name");
			if (isNull(name)) {
				return true;
			}
			var value = $(this).val();
			if (!isNull(value)) {
				value = $.trim(value);
				params[name] = value;
				params["pageSize"] = 10;
				params["status"]=2;
			} else {
				params["level"] = 1;
				params["pageSize"] = 10;
				params["status"]=2;
			}
		});
		moduleTable.post(params, 0);
	});
}